<div class="modal fade " id="addScheduleTaskModal"  role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">任务维护</h3>
            </div>

            <form class="form-horizontal" role="form" id="addScheduleTaskForm"  method="post" action="/task/" enctype="multipart/form-data">
                <div class="box-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="idLabel">序号</label>
                        <div class="col-sm-4">
                            <input id="id" name="id" type="text" disabled="disabled" class="form-control">
                        </div>
                        <label class="col-sm-2 control-label" for="nameLabel">名称</label>
                        <div class="col-sm-4">
                            <input id="name" name="name" type="text" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="portLabel">端口</label>
                        <div class="col-sm-4">
                            <input id="port" name="port" type="text" class="form-control"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                        <label class="col-sm-2 control-label" for="urlLabel">URL</label>
                        <div class="col-sm-4">
                            <input id="url" name="url" type="text" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="startLabel">开始时间</label>
                        <div class="col-sm-4">
                            <input id="startTime" name="startTime" type="text" class="form-control" placeholder="hhmmss"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                        <label class="col-sm-2 control-label" for="endLabel">结束时间</label>
                        <div class="col-sm-4">
                            <input id="endTime" name="endTime" type="text" class="form-control" placeholder="hhmmss"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="intervalLabel">间隔</label>
                        <div class="col-sm-4">
                            <!--<input id="interval" name="interval" type="text" class="form-control"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">-->
                            <input type="checkbox"  id="day" value="5"  checked="checked"/>日
                            <label> &nbsp;</label>
                            <input type="checkbox"  id="month" value="2"/>月
                            <label> &nbsp;</label>
                            <input type="checkbox"  id="year" value="1"/>年
                        </div>
                        <label class="col-sm-2 control-label" for="nextDateLabel">下一执行日</label>
                        <div class="col-sm-4">
                            <input id="nextDate" class="form-control" placeholder="YYYY-MM-DD" onClick="WdatePicker()">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="isDependencyLabel">是否依赖</label>
                        <div class="col-sm-4">
                            <select id="isDependency" name="isDependency" class="form-control">
                                <option value="0" selected="selected">不依赖</option>
                                <option value="1">依赖</option>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="dependencyIdLabel">依赖序号</label>
                        <div class="col-sm-4">
                            <input id="dependencyId" name="dependencyId" type="text" class="form-control"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="isHolidayLabel">假日执行</label>
                        <div class="col-sm-4">
                            <select id="isHoliday" name="isHoliday" class="form-control">
                                <option value="0" selected="selected">否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="statusLabel">执行状态</label>
                        <div class="col-sm-4">
                            <select id="status" name="status" class="form-control">
                                <option value="1" selected="selected">有效</option>
                                <option value="0">无效</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="runTypeLabel">执行类型</label>
                        <div class="col-sm-4">
                            <select id="runType" name="runType" class="form-control">
                                <option value="1" selected="selected">重复运行</option>
                                <option value="2">一次运行</option>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="runEnvLabel">执行环境</label>
                        <div class="col-sm-4">
                            <input type="checkbox"  id="prod" value="prod"  checked="checked"/>Prod
                            <label> &nbsp;</label>
                            <input type="checkbox"  id="dev" value="dev"/>Dev
                            <label> &nbsp;</label>
                            <input type="checkbox"  id="local" value="local"/>Local
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="httpMethodLabel">请求类型</label>
                        <div class="col-sm-4">
                            <input id="httpMethod" name="httpMethod" type="text" class="form-control">
                        </div>
                        <label class="col-sm-2 control-label" for="dealIntervalLabel">执行间隔</label>
                        <div class="col-sm-4">
                            <input id="dealInterval" name="dealInterval" type="text" class="form-control"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="errorTimesLabel">失败次数</label>
                        <div class="col-sm-4">
                            <input id="errorTimes" name="errorTimes" type="text" class="form-control"
                                   onkeyup= "if( /[^\d]/g.test(this.value)){alert('只能整数');this.value='';}">
                        </div>
                        <label class="col-sm-2 control-label" for="propertityLabel">属性</label>
                        <div class="col-sm-4">
                            <input id="propertity" name="propertity" type="text" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remarkLabel">备注</label>
                        <div class="col-sm-10">
                            <input id="remark" name="remark" type="text" class="form-control">
                        </div>
                    </div>
                </div>
                <div class="box-footer">
                    <button type="button" class="btn btn-danger pull-left" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary pull-right" onclick="saveTask()">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="dist/js/datepicker/WdatePicker.js" language="javascript" type="text/javascript" ></script>
<script>
    function addScheduleTaskInit() {
        $("#id").val("");
        $("#name").val("");
        $("#port").val("");
        $("#url").val("");
        $("#startTime").val("");
        $("#endTime").val("");
        $('#day').attr('checked',true)
        $('#month').attr('checked',false)
        $('#year').attr('checked',true)
        $("#nextDate").val("");
        $("#isDependency").val("0");
        $("#dependencyId").val("");
        $("#isHoliday").val("0");
        $("#status").val("1");
        $("#runType").val("1");
        $('#prod').attr('checked',true)
        $('#dev').attr('checked',false)
        $('#local').attr('checked',false)
        $("#httpMethod").val("");
        $("#dealInterval").val("");
        $("#errorTimes").val("");
        $("#propertity").val("");
        $("#remark").val("");
    }

    function modifyScheduleTaskInit(data){
        $("#id").val(data.id);
        $("#name").val(data.taskName);
        $("#port").val(data.systemPort);
        $("#url").val(data.interfaceUrl);
        $("#startTime").val(data.beginTime);
        $("#endTime").val(data.endTime);

        //这里checkbox设置只能用prop,如果用attr，第二次会失效
        //设置间隔
        $('#day').prop('checked',false)
        $('#month').prop('checked',false)
        $('#year').prop('checked',false)

        var interval = data.interval.split("|")
        for (var i = 0; i < interval.length; i++) {
            if(interval[i] == '5'){
                $('#day').prop('checked',true);
                continue;
            }
            if(interval[i] == '2'){
                $('#month').prop('checked',true);
                continue;
            }
            if(interval[i] == '1'){
                $('#year').prop('checked',true);
                continue;
            }
        }

        $("#nextDate").val(data.nextDate);
        $("#isDependency").val(data.isDependency);
        $("#dependencyId").val(data.dependencyTaskId);
        $("#isHoliday").val(data.isExeHoliday);
        $("#status").val(data.status);
        $("#runType").val(data.runType);

        //设置运行环境
        $('#prod').prop('checked',false);
        $('#dev').prop('checked',false);
        $('#local').prop('checked',false);
        var runEnv = data.runEnv.split(",");

        for (var i = 0; i < runEnv.length; i++) {
            if(runEnv[i] == 'prod'){
                $('#prod').prop('checked',true);
                continue;
            }
            if(runEnv[i] == 'dev'){
                $('#dev').prop('checked',true);
                continue;
            }
            if(runEnv[i] == 'local'){
                $('#local').prop('checked',true);
                continue;
            }
        }

        $("#httpMethod").val(data.httpMethod);
        $("#dealInterval").val(data.dealInterval);
        $("#errorTimes").val(data.errorTimes);
        $("#propertity").val(data.properties);
        $("#remark").val(data.remark);
    }

    function saveTask(){
       //任务序号
        var id = $("#id").val();
        //任务名
        var name = $("#name").val();
        //服务端口
        var port = $("#port").val();

        if(port.trim() == ""){
            toastr['error']("请设置服务端口", '提示');
            return;
        }
        //服务URL
        var url = $("#url").val();

        if(url.trim() == ""){
            toastr['error']("请设置服务URL", '提示');
            return;
        }
        //开始时间
        var startTime = $("#startTime").val();
        if(startTime.trim() == ""){
            toastr['error']("请设置执行开始时间", '提示');
            return;
        }
        if(startTime < 0 || startTime > 235959){
            toastr['error']("开始时间设置需在000000~235959区间", '提示');
            return;
        }

        //结束时间
        var endTime = $("#endTime").val();
        if(endTime.trim() == ""){
            toastr['error']("请设置执行结束时间", '提示');
            return;
        }
        if(endTime < 0 || endTime > 235959){
            toastr['error']("结束时间设置需在000000~235959区间", '提示');
            return;
        }

        //间隔
        var interval = "";
        if($("#day").is(':checked')){
            if(interval == ''){
                interval = $("#day").attr('value');
            }
            else{
                interval = interval + "|" + $("#day").attr('value')
            }
        }
        if($("#month").is(':checked')){
            if(interval == ''){
                interval = $("#month").attr('value');
            }
            else{
                interval = interval + "|" + $("#month").attr('value')
            }
        }
        if($("#year").is(':checked')){
            if(interval == ''){
                interval = $("#year").attr('value');
            }
            else{
                interval = interval + "|" + $("#year").attr('value')
            }
        }

        if(interval.trim() == ""){
            toastr['error']("请设置间隔", '提示');
            return;
        }

        //下一执行日
        var nextDate = $("#nextDate").val();
        if(nextDate.trim() == ""){
            toastr['error']("请设置下一执行日", '提示');
            return;
        }

        //是否依赖
        var isDependency = $('#isDependency option:selected').val();
        //依赖任务序号
        var dependencyId = $("#dependencyId").val();

        if(isDependency == "1" && dependencyId.trim() == ""){
            toastr['error']("请设置依赖任务ID", '提示');
            return;
        }

        //假日是否执行
        var isHoliday = $('#isHoliday option:selected').val();
        //执行状态
        var status = $('#status option:selected').val();
        //执行类型
        var runType = $('#runType option:selected').val();
        //执行环境
        var runEnv = "";
        if($("#prod").is(':checked')){
            if(runEnv == ''){
                runEnv = $("#prod").attr('value');
            }
            else{
                runEnv = runEnv + "," + $("#prod").attr('value')
            }
        }
        if($("#dev").is(':checked')){
            if(runEnv == ''){
                runEnv = $("#dev").attr('value');
            }
            else{
                runEnv = runEnv + "," + $("#dev").attr('value')
            }
        }
        if($("#local").is(':checked')){
            if(runEnv == ''){
                runEnv = $("#local").attr('value');
            }
            else{
                runEnv = runEnv + "," + $("#local").attr('value')
            }
        }
        //请求方式
        var httpMethod = $("#httpMethod").val();
        //执行间隔
        var dealInterval = $("#dealInterval").val();
        //if(dealInterval.trim() == ""){
        //    toastr['error']("请设置执行间隔", '提示');
        //    return;
        //}
        //执行错误次数
        var errorTimes = $("#errorTimes").val();
        //属性
        var propertity = $("#propertity").val();
        //备注
        var remark = $("#remark").val();

        var data = {
            id : id,
            taskName : name,
            systemPort : port,
            interfaceUrl : url,
            beginTime : startTime,
            endTime : endTime,
            interval : interval,
            nextDate : nextDate,
            isExeHoliday : isHoliday,
            isDependency : isDependency,
            dependencyTaskId : dependencyId,
            status : status,
            remark : remark,
            runType : runType,
            runEnv : runEnv,
            httpMethod : httpMethod,
            properties : propertity,
            dealInterval : dealInterval,
            errorTimes : errorTimes
        };

        var url = "/task/maintain";
        postReq(url, data, function (result) {
            if (result.success) {
                addScheduleTaskInit();
                $("#taskTable").bootstrapTable("refresh");
                $("#addScheduleTaskModal").modal("hide");
                toastr['success']('任务保存成功', '提示');
            } else {
                toastr['error'](result.msg, '提示');
            }
        });
    }
</script>
